package cn.bgs.serviceedu.controller;


import cn.bgs.serviceedu.clients.Vodclients;
import cn.bgs.serviceedu.entity.Doctor;
import cn.bgs.serviceedu.entity.Hospital;
import cn.bgs.serviceedu.entity.Video;
import cn.bgs.serviceedu.service.VideoService;
import cn.bgs.serviceedu.vo.RedisList;
import cn.bgs.serviceedu.vo.VodList;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import cn.bgs.serviceutils.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YiChen
 * @since 2021-11-16
 */
@Api(description = "视频点播")
@RestController
@RequestMapping("/serviceedu/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private Vodclients vodclients;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 查询视频
     * @return
     */
    @ApiOperation(value = "查询视频")
    @CachePut(key = "'VodList'",value = "VodList")
    @GetMapping("/findAll")
    public R findAll(){
      return  R.ok().data("list",videoService.list());
    }


    /**
     * 根据id播放视频
     * @param vodId
     * @return
     */
    @ApiOperation(value = "根据id播放视频")
    @GetMapping("/findById/{vodId}")
    public R findById(@PathVariable Integer vodId){
        return   R.ok().data("list",videoService.getById(vodId));
    }



    /**
     * 修改用户
     * @param video
     * @return
     */
    @ApiOperation(value = "修改视频")
    @PutMapping("/update")
    public R update(@RequestBody Video video){
        boolean update = false;
        try {
            update = videoService.updateById(video);
        } catch (Exception e) {
            throw new CSException(20001,"修改失败");
        }
        if (update){
            return  R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    /**
     * 视频播放量
     * @param vodId
     * @return
     */
    @ApiOperation(value = "视频播放量")
    @GetMapping("/browse/{vodId}")
    public R browse(@PathVariable Integer vodId){
        String key ="video:"+vodId;
        long incr = redisUtils.incr(key, 1);
        return R.ok().data("count",incr);
    }


    /**
     * 手动持久化数据
     * @return
     */
    @ApiOperation(value = "手动持久化数据")
    @GetMapping("/add")
    public R add(){
        List<VodList> vodLists = videoService.getRedisList();
        Video video = new Video();
        try {
            for (int i = 0; i < vodLists.size(); i++) {
                VodList redisList1 = vodLists.get(i);
                Video byId = videoService.getById(redisList1.getVodId());
                if (!byId.getPlayCount().equals(redisList1.getPlayCount())) {
                    video.setVodId(Integer.valueOf(redisList1.getVodId()));
                    video.setPlayCount(redisList1.getPlayCount());
                    videoService.updateById(video);
                }
            }
        } catch (NumberFormatException e) {
            throw new CSException(20001,"修改失败");
        }
        return R.ok();
    }

    /**
     * 添加视频
     * @param video
     * @return
     */
    @ApiOperation(value = "添加点播视频")
    @PostMapping("/addVod")
    public R addVod(@RequestBody Video video){
        boolean save = false;
        try {
            save = videoService.save(video);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (save){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    /**
     * 根据ID删除
     * @param vodId
     * @return
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("/del/{vodId}")
    public R del(@PathVariable Integer vodId){
        Video byId = videoService.getById(vodId);

        boolean b = false;
        try {
           vodclients.removeVideo(byId.getVideoSourceId());
            b = videoService.removeById(vodId);
        } catch (Exception e) {
            throw new CSException(20001,"删除失败");
        }
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

}
